package logic

import (
	"context"
	"house/house"

	"api/internal/svc"
	"api/internal/types"
	"github.com/golangblogs/gojwt"
	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *LoginLogic) Login(req *types.LoginRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	login, err := l.svcCtx.HouseSrv.Login(l.ctx, &house.LoginRequest{
		Mobile:   req.Mobile,
		Password: req.Password,
		SendSms:  req.SendSms,
	})
	if err != nil {
		return &types.Response{
			Code:     500,
			Messages: err.Error(),
			Data:     nil,
		}, nil
	}
	newJWT := jwt.NewJWT("2302a")
	claims := jwt.CustomClaims{ID: uint(login.Id)}
	token, err := newJWT.CreateToken(claims)
	if err != nil {
		return nil, err
	}
	return &types.Response{
		Code:     500,
		Messages: "发送成功",
		Data:     token,
	}, nil
}
